/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.system.dto.SystemLogPageDTO;
import com.pig4cloud.pigx.system.entity.SystemLog;
import com.pig4cloud.pigx.system.enums.SystemLogEnum;
import com.pig4cloud.pigx.system.mapper.SystemLogMapper;
import com.pig4cloud.pigx.system.service.SystemLogService;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 系统日志
 *
 * @since 2022-11-23 15:15:20
 */
@Service
@RequiredArgsConstructor
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog> implements SystemLogService {

    /**
     * 分页查询
     *
     * @param page      分页条件
     * @param condition 查询条件
     * @return {@link Page < SystemLog>}
     * @since 2022/11/24 10:48
     */
    @Override
    public Page<SystemLog> page(Page<SystemLog> page, SystemLogPageDTO condition) {
        QueryWrapper<SystemLog> query = new QueryWrapper<>();
        query.lambda()
                .eq(!Objects.isNull(condition.getLogId()), SystemLog::getLogId, condition.getLogId())
                .eq(Strings.isNotBlank(condition.getOperatorType()), SystemLog::getOperatorType, condition.getOperatorType())
                .like(Strings.isNotBlank(condition.getType()), SystemLog::getType, condition.getType())
                .apply(!Objects.isNull(condition.getStartTime()), "timeStampDiff(SECOND, create_time, {0}) <= 0", condition.getStartTime())
                .apply(!Objects.isNull(condition.getEndTime()), "timeStampDiff(SECOND, create_time, {0}) >= 0", condition.getEndTime())
                .orderByDesc(SystemLog::getCreateTime);

        page = baseMapper.selectPage(page, query);
        List<SystemLog> list = page.getRecords();

        // 遍历
        list.forEach(i -> {
            // 翻译字典项
            i.setOperatorType(SystemLogEnum.OperatorType.findName(i.getOperatorType()));
        });

        return page;
    }
}
